import datetime
import re

import scrapy

'''
爬取目标：csdn上的个人博客
爬取信息-item：
1、博客标题：  title
2、发布时间：   publish
3、点赞量：      approval
4、踩:        unlike
5、评论量：      comment
6、收藏量：      collection

'''


class PblogSpider1(scrapy.Spider):
    name = 'pblog1'
    allowed_domains = ['blog.csdn.net']
    start_urls = ['https://blog.csdn.net/gaogzhen/article/list/1']

    def parse(self, response):
        # 提取全部连接
        links = response.css('#articleMeList-blog>.article-list h4>a::attr(href)').getall()
        # 解析每个连接详情页
        for link in links:
            yield response.follow(link, self.parse_detail)

    def parse_detail(self, response):
        item = {}
        # 解析文章标题
        item['title'] = response.css('.main_father .container main .blog-content-box .article-title-box>h1::text').get()
        # 解析发布时间
        # 获取的时间有额外的信息需要正则过滤下
        publish_string = re.search('于 (.*) 发布', response.css('.main_father .container main .blog-content-box .bar-content>span.time::text').get()).group(1)
        if publish_string is None or publish_string == '':
            publish = datetime.datetime.now()
        else:
            publish = datetime.datetime.strptime(publish_string, "%Y-%m-%d %H:%M:%S")
        item['publish'] = publish
        # 解析点赞
        item['approval'] = response.css('span#spanCount::text').get()
        # 解析踩
        item['unlike'] = response.css('span#unlikeCount::text').get()
        # 解析评论
        item['comment'] = response.css('.main_father .container main .more-toolbox-new .toolbox-middle li.tool-item-comment a span.count::text').get()
        # 解析收藏
        item['collection'] = response.css('span#get-collection::text').get()

        return item
